/**********************************************************************
 * FILE : ProcessLancher.java
 * CREATE DATE : 2011-12-2
 * DESCRIPTION :
 *		
 *      
 * CHANGE HISTORY LOG
 *---------------------------------------------------------------------
 * NO.|    DATE    |     NAME     |     REASON     | DESCRIPTION
 *---------------------------------------------------------------------
 * 1  | 2011-12-2 |  Sting  |    草创版本
 *---------------------------------------------------------------------              
 **********************************************************************
 */
package com.sgm.de.benchmark.lancher;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.sgm.de.benchmark.Cmd;
import com.sgm.de.benchmark.Cmder;
import com.sgm.de.benchmark.JMSProcesser;
import com.sgm.de.benchmark.ProcessConfig;

/**
 * @author Sting
 */
public class ProcessLancher {
    public static void main(String[] args) throws IOException {
        JMSProcesser processer = new JMSProcesser(true);
        String all = getFileContent("config.json");
        if (all != null) {
            ProcessConfig config = JSON.parseObject(all, ProcessConfig.class);
            processer.commandLocal(Cmder.config(config));
        }
        if (args.length > 0) {
            loadCommand(processer, args[0]);
        }

        final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println("load command file :" + line + ".json");
            loadCommand(processer, line);
        }

    }

    /**
     * TODO 新建方法
     * 
     * @param processer
     * @param line
     */
    public static void loadCommand(JMSProcesser processer, String line) {
        String all;
        all = getFileContent(line + ".json");
        if (all != null) {
            List<Cmd> cmds = JSON.parseArray(all, Cmd.class);
            for (Cmd cmd : cmds) {
                processer.commandLocal(JSON.toJSONString(cmd));
            }
        } else {
            System.out.println("cant find file");
        }
    }

    public static String getFileContent(String filename) {
        File f = new File(filename);
        System.out.println(f.getAbsolutePath());
        FileReader is;
        try {
            is = new FileReader(f);
        } catch (FileNotFoundException e1) {
            System.err.printf("cant find file %s", filename);
            return null;
        }
        BufferedReader br = new BufferedReader(is);
        String line = "", all = "";
        try {
            while ((line = br.readLine()) != null) {
                all += line + "\n";
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return all;
    }
}
